package edu.gatech.oad.mule;

import java.awt.Dimension;

import edu.gatech.oad.mule.config.interfaces._ConfigurationPanel;
import edu.gatech.oad.mule.view.interfaces.RenderableMap;

/**
 * This map represents the town
 * 
 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin
 *         Park
 * @version %I%, %G%
 */
public class TownMap extends Map implements RenderableMap {

	private static final long serialVersionUID = -8096244482105348199L;

	// outfitters
	private Outfitter crystite;
	private Outfitter smithore;
	private Outfitter energy;
	private Outfitter food;

	// assay office
	//

	// land office
	//
	
	//town store
	private Store store;

	// bar
	private Bar bar;

	// MULE stable
	private MULEStable stable;

	/**
	 * This constructor creates a new town map object
	 */
	public TownMap(_ConfigurationPanel cp) {
		System.out.println("townmap");
		/*
		 * | Crystite | Smithore |x| Energy | Food |
		 * ----------------------+
		 * +------------------ x x ----------------------+ +------------------ 
		 * | Assay | Land |x| Bar | Stable |
		 */

		// create 12 zones. There are 8 buildings, so one
		// zone for each building, plus the 4 exits
		// (marked by the x's)
		zoneGrid = new Zone[12];

		// this calculates the height and width of the buildings.
		// the town map is the same dimension as the world map.
		// there are 4 buildings up top, and 4 down on the bottom.
		// in addition, there are lanes for the player to walk
		// around and a 1 tile sized exit in each of the walkways.
		int bldgWidth = (WorldMap.WIDTH - Tile.WIDTH) / 4;
		int bldgHeight = (WorldMap.HEIGHT - Tile.HEIGHT) / 2;

		// MULE outfitters
		crystite = new Outfitter(0, 0, bldgWidth, bldgHeight, MULE.CRYSTITE);
		zoneGrid[0] = crystite;
		smithore = new Outfitter(bldgWidth, 0, bldgWidth, bldgHeight,
				MULE.SMITHORE);
		zoneGrid[1] = smithore;
		energy = new Outfitter(bldgWidth * 2 + Tile.WIDTH, 0, bldgWidth,
				bldgHeight, MULE.ENERGY);
		zoneGrid[2] = energy;
		food = new Outfitter(bldgWidth * 3 + Tile.WIDTH, 0, bldgWidth,
				bldgHeight, MULE.FOOD);
		zoneGrid[3] = food;
		
		// assayOffice
		// 4
		
		// landOffice
		// 5
		
		//bar
		bar = new Bar(bldgWidth * 2 + Tile.WIDTH, bldgHeight + Tile.HEIGHT,
				bldgWidth, bldgHeight);
		zoneGrid[6] = bar;
		
		//stable
		stable = new MULEStable(bldgWidth * 3 + Tile.WIDTH, bldgHeight
				+ Tile.HEIGHT, bldgWidth, bldgHeight, cp);
		zoneGrid[7] = stable;
		
		//store
		store = new Store(bldgWidth, bldgHeight + Tile.HEIGHT, bldgWidth, bldgHeight, cp);
		zoneGrid[5] = store;
		
		//exits
		zoneGrid[8] = new Exit(bldgWidth * 2, 0, Tile.WIDTH, Tile.WIDTH); //upper
		zoneGrid[9] = new Exit(0, bldgHeight, Tile.WIDTH, Tile.WIDTH); //left
		zoneGrid[10] = new Exit(bldgWidth * 2, WorldMap.HEIGHT - Tile.HEIGHT, Tile.WIDTH, Tile.WIDTH); //bottom
		zoneGrid[11] = new Exit(WorldMap.WIDTH - Tile.WIDTH, bldgHeight, Tile.WIDTH, Tile.WIDTH); //right
		
		System.out.println("/townmap");
	}// end constructor
	
	//TODO delete this after JUnit test cases
	public TownMap(Store store, MULEStable stable) {
		this.store = store; this.stable = stable;
	}
	
	/**
	 * This method calculates the production for the TownMap.  If
	 * it has enough smithore, it will make a few MULEs.
	 */
	public void production(){
		System.out.print("Calculating production for the Town. ");
		System.out.print("Town currently has [" + store.getSmithore() + "] smithore and ");
		System.out.print("[" + stable.getMULECount() + "] MULEs. ");
		//it takes 2 smithore to make a MULE, as long as we have at least 2
		//  smithore we can produce MULEs
		while(store.getSmithore() >= 2){
			System.out.println("Creating MULE...");
			//use two smithore to make a MULE
			store.addSmithore(-2);
			stable.addMULE();
		}//end if
		System.out.print("Town now has [" + store.getSmithore() + "] smithore and ");
		System.out.print("[" + stable.getMULECount() + "] MULEs. ");
	}//end method production
	
	// ==============================
	// Map
	// ============================== 

	@Override
	public Zone getCollidedZone(Player p) {
		for (int i = 0; i < zoneGrid.length; i++) {
			if (zoneGrid[i] != null) {
				if (zoneGrid[i].contains(p)) {
					return zoneGrid[i];
				}// end if
			}
		}// end for
		return null;
	}// end method getCollidedZone

	// ==============================
	// RenderableMap
	// ==============================

	@Override
	public MapType getMapType() {
		return MapType.TOWN;
	}// end method getMapType

	@Override
	public Zone[] getZones() {
		return zoneGrid;
	}// end method getZones

	@Override
	public Dimension getSize() {
		return new Dimension(WorldMap.WIDTH, WorldMap.HEIGHT);
	}//end method getSize

}// end class TownMap
